<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Budjeting extends MX_Controller {
  function __construct() {
    $this->menu = $this->cek();
  }
	
	public function settings_account(){
//    $this->debug($this->input->post(NULL, TRUE), true);
    if(!$this->input->post(NULL, TRUE)){
      $budjet = $this->global_models->get("budjeting_account");
      $this->template->build('settings-account', 
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'budjeting/settings-account',
            'title'       => lang("budjeting_setting_account"),
            'budjet'      => $budjet
          ));
      $this->template
        ->set_layout('default')
        ->build('settings-account');
    }
    else{
      $pst = $this->input->post(NULL, TRUE);
//      $this->debug($pst, true);
      if($pst){
        $this->global_models->truncate('budjeting_account');
        foreach($pst['percent'] AS $key => $percent){
          $kirim[] = array(
              "id_frm_account"        => $pst['id_frm_account'][$key],
              "persen"                => $percent,
              "create_by_users"       => $this->session->userdata("id"),
              "create_date"           => date("Y-m-d H:i:s")
          );
        }
        $this->global_models->insert_batch("budjeting_account", $kirim);
        redirect ("budjeting/settings-account");
      }
    }
  }
  
  public function add_row(){
    $no = rand(100, 100000);
    $isi = $this->form_eksternal->form_input("percent[]", "").
                                  $this->form_eksternal->form_autocomplate("frm_account[]", "id_frm_account[]", "frm_account_{$no}", "id_frm_account_{$no}", site_url("frm/accounting/auto_account"), 0, 0);
    print <<<EOD
    <tr id='tr_{$no}'><td>{$isi}</td>
    <td><a href='javascript:void(0)' class='remove-element' onclick='del_row_tambahan("tr_{$no}")' title='Remove' >Remove</a></td></tr>
EOD;
    die;
  }
  
  function index(){
    $data = $this->global_models->get_query("
      SELECT year
      FROM frm_journal
      GROUP BY year
      ORDER BY year ASC
      ");
    $this->template->build('main', 
    array(
          'url'         => base_url()."themes/".DEFAULTTHEMES."/",
          'menu'        => 'budjeting',
          'title'       => lang("budjeting"),
          'data'        => $data
        ));
    $this->template
      ->set_layout('default')
      ->build('main');
  }
  
  function detail_budjeting($year){
    $account = $this->global_models->get_query("
      SELECT A.*, B.pos, B.title, B.nomor
      FROM budjeting_account AS A
      LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
      ");
    foreach ($account AS $key => $value){
      $log = $this->global_models->get_query("
        SELECT sum(saldo) AS nilai
        FROM frm_journal_log_account
        WHERE id_frm_account = {$value->id_frm_account} AND pos = {$value->pos}
        AND id_frm_journal IN (SELECT id_frm_journal FROM frm_journal WHERE year = {$year})
        ");
      $account[$key]->saldo = $log[0]->nilai * $value->persen / 100;
    }
    
//    $this->debug("SELECT sum(saldo)
//        FROM frm_journal_log_account
//        WHERE id_frm_account = {$value->id_frm_account} AND pos = {$value->pos}
//        AND id_frm_journal IN (SELECT id_frm_journal FROM frm_journal WHERE year = {$year})");
//    $this->debug($account, true);
    
    $this->template->build('detail-budjeting', 
    array(
          'url'         => base_url()."themes/".DEFAULTTHEMES."/",
          'menu'        => 'budjeting',
          'title'       => lang("budjeting"),
          'data'        => $account
        ));
    $this->template
      ->set_layout('default')
      ->build('detail-budjeting');
  }
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */